package com.seh.contentcenter.controller;

import cn.hutool.core.bean.BeanUtil;
import com.seh.contentcenter.domain.Result;
import com.seh.contentcenter.dto.AddressInfoDTO;
import com.seh.contentcenter.entity.AddressInfo;
import com.seh.contentcenter.service.Impl.AddressServiceImpl;
import com.seh.contentcenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressInfoController {
    @Autowired
    private AddressServiceImpl addressService;

    /**
     * 通过用户id，查询此用户所有保存的地址
     *
     * @param userId
     * @return
     */
    @GetMapping("/findAddressByUserId/{userId}")
    public List<AddressInfoDTO> findAddressByUserId(@PathVariable Integer userId) {
        List<AddressInfoDTO> addressByUserId = addressService.findAddressByUserId(userId);
        return addressByUserId;
    }

    /**
     * 通过地址主键，查看地址的详细信息
     *
     * @param addressId
     * @return
     */
    @GetMapping("/findAddressByAddressId/{addressId}")
    public AddressInfoDTO findAddressByAddressId(@PathVariable Integer addressId) {
        AddressInfoDTO addressById = addressService.findAddressById(addressId);
        return addressById;

    }

    /**
     * 添加新的地址
     *
     * @param addressInfo
     * @return
     */
    @PostMapping("/addAddress")
    public String addAddress(@RequestBody AddressInfo addressInfo) {
        return addressService.addNewAddress(addressInfo);

    }

    /**
     * 批量添加新的地址
     *
     * @param addressInfoList
     * @return
     */
    @PostMapping("/addAddresses")
    public String addAddresses(@RequestBody List<AddressInfoDTO> addressInfoList) {
        return addressService.addNewAddresses(addressInfoList);
    }

    /**
     * 通过主键id，删除地址信息
     *
     * @param addressId
     * @return
     */
    @GetMapping("/deleteAddress/{addressId}")
    public String deleteAddress(@PathVariable Integer addressId) {
        return addressService.deleteAddressById(addressId);
    }

    /**
     * 修改地址信息
     *
     * @param addressInfoDTO
     * @return
     */
    @PostMapping("/updateAddress")
    public String updateAddress(@RequestBody AddressInfoDTO addressInfoDTO) {
        String s = addressService.updateAddressInfo(addressInfoDTO);
        return s;
    }

}
